<template>
	<div>
		<table>
			<thead>
				<td>序号</td>
				<td>名称</td>
				<td>name</td>
			</thead>
			<tbody>
				<tr v-for="item in dataList">
					<td>{{ item.sort }}</td>
					<td>{{ item.spuName }}</td>
					<td>{{ item.skuName }}</td>
				</tr>
			</tbody>
		</table>
		<button @click="exports()">导出excel</button>
	</div>
</template>

<script>
export default {
	data() {
		return {
			dataList: [
				{
					sort: 1,
					spuCode: '14523123',
					spuName: '2',
					skuName: 'w',
					boxName: 'a',
					sum: '14523123',
					boxNum: '14523123',
					skuNum: '123'
				},
				{
					sort: 2,
					spuCode: '14523123',
					spuName: '2',
					skuName: 'w',
					boxName: 'a',
					sum: '14523123',
					boxNum: '14523123',
					skuNum: '123'
				},
				{
					sort: 3,
					spuCode: '14523123',
					spuName: '2',
					skuName: 'w',
					boxName: 'a',
					sum: '14523123',
					boxNum: '14523123',
					skuNum: '123'
				}
			]
		};
	},
	mounted() {},
	methods: {
		exports() {
			const { export_json_to_excel } = require('@/assets/Export2Excel');
			// 表头  需要的字段表头名称
			const tableHeader = ['序号', '货号', '商品名称'];
			// 需要过滤出来的字段  需要的字段
			const filterVal = ['sort', 'spuCode', 'spuName'];
			// 筛选数据
			const data = this.formatJson(filterVal, this.dataList);
			const fileName = '测试Excel' + this.$formatDate();
			console.log(fileName);
			// 这里才是进行导出
			export_json_to_excel(tableHeader, data, fileName);
		},
		// 先循环 数据列表  每次数据列表在循环 只return 出需要的字段
		formatJson(filterVal, jsonData) {
			return jsonData.map(v => filterVal.map(j => v[j]));
		}
	}
};
</script>

<style scoped></style>
